package com.wcgksoft.mobileservice.Account.greedLand;

import com.google.gson.JsonObject;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.GreedLand.GreedLandPhoto;
import com.wcgksoft.entity.Plant.Plant;
import com.wcgksoft.entity.Plant.PlantEncyclopedias;
import com.wcgksoft.service.GreedLand.GreedlandPhotoService;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.plant.PlantEncyclopediasService;
import com.wcgksoft.service.plant.PlantService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 绿地管理
 *
 * @return
 */
@Controller
@RequestMapping(value = "mobile/greedLand")
public class AppGreedLandController {
    @Autowired
    private GreedlandService greedlandService;

    @Autowired
    private PlantService plantService;

    @Autowired
    private PlantEncyclopediasService plantEncyclopediasService;
    @Autowired
    private GreedlandPhotoService greedlandPhotoService;

    /**
     * 绿地管理列表-
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public MsgInfo list(
            @RequestParam(value = "curPage", defaultValue = "1", required = false) Integer curPage,
            @RequestParam(value = "pageSize",defaultValue = "8", required = false) Integer pageSize,
            @RequestParam(value = "proid", required = false) String proid,
            @RequestParam(value = "areaid", required = false) String areaid
    ) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));

        List<GreedLand> list = greedlandService.findByConditions(criteria,curPage-1,pageSize);
        for (GreedLand greedLand : list) {
            DetachedCriteria criteria2 = DetachedCriteria.forClass(GreedLandPhoto.class);
            criteria2.add(Restrictions.eq("greedId",greedLand.getId()));
            List<GreedLandPhoto> byConditions = greedlandPhotoService.findByConditions(criteria2);
            greedLand.setGreedLandPhotoList(byConditions);
        }
        if(list.size()>0){
            msgInfo.setCode((int) Math.ceil(list.size()/8)+1);
            msgInfo.setMsg("查询成功");
            msgInfo.setData(list);
        }else{
            msgInfo.setCode(1);
            msgInfo.setMsg("无数据");
            msgInfo.setData(list);
        }

        return msgInfo;
    }


    /**
     * 绿地概览
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="view")
    public MsgInfo view(@RequestParam(value = "id") Integer id){
        MsgInfo msgInfo = new MsgInfo();
        GreedLand byId = greedlandService.findById(id);

        DetachedCriteria criteria2 = DetachedCriteria.forClass(GreedLandPhoto.class);
        criteria2.add(Restrictions.eq("greedId",byId.getId()));
        List<GreedLandPhoto> byConditions = greedlandPhotoService.findByConditions(criteria2);
        byId.setGreedLandPhotoList(byConditions);

        msgInfo.setData(byId);
        return msgInfo;
    }

    /**
     * 绿地统计
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="statistics")
    public MsgInfo statistics(
            @RequestParam(value = "proid", required = false) String proid,
            @RequestParam(value = "areaid", required = false) String areaid,
            @RequestParam(value = "enterpriseid", required = false) Integer enterpriseid
    ){
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));
        criteria.add(Restrictions.eq("state", 0));
        List<GreedLand> list = greedlandService.findByConditions(criteria);
        JsonObject test=new JsonObject();
       if(list.size()>0){
           int area=0;
           for(int i=0;i<list.size();i++){
                area+=list.get(i).getGrossLandArea();

           }
           DetachedCriteria criteriae = DetachedCriteria.forClass(PlantEncyclopedias.class);
           criteriae.add(Restrictions.eq("enterpriseid", enterpriseid));
           List<PlantEncyclopedias> liste = plantEncyclopediasService.findByConditions(criteriae);

           DetachedCriteria criteriat = DetachedCriteria.forClass(Plant.class);
           criteriat.add(Restrictions.eq("departid", proid));
           criteriat.add(Restrictions.eq("areaid", areaid));
           List<Plant> listt = plantService.findByConditions(criteriat);
          int plant=0;
            if(liste.size()>0){
                for(int i=0;i<liste.size();i++){
                        if(listt.size()>0){
                            for(int j=0;j<listt.size();j++){
                                    if(liste.get(i).getId().equals(listt.get(j).getPlantEncyclopedias().getId())){
                                        plant=plant+1;
                                        break;
                                    }
                            }
                        }
                }
            }


           test.addProperty("greed", list.size());
           test.addProperty("area", area);
           test.addProperty("plant", plant);

       }

       msgInfo.setData(test.toString());
        return msgInfo;
    }

}
